# -*- coding:utf-8 -*-
from selenium import webdriver
import time
import configparser
import os

options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
driver = webdriver.Chrome(
    executable_path='F:/crawler/chromedriver_win32/chromedriver.exe',
    options=options
)

config_parser =  configparser.ConfigParser()
config_parser.read( "news.cfg", encoding='utf-8')

driver.get("http://127.0.0.1:8000")

# 网站编码 0:GB2312; 1:UTF-8 ; 2:None
encode_flag = int(config_parser.get("news_config","encoding"))
driver.find_elements_by_class_name("uni-radio-input")[encode_flag].click()

# 网页类型 3:单页; 4:多页 ; 5:None
type_flag = 4
driver.find_elements_by_class_name("uni-radio-input")[type_flag].click()

time.sleep(3)

# 项目名字
pro_name = config_parser.get("news_config","project_name")
driver.find_elements_by_tag_name("input")[0].send_keys(pro_name)

# 网址 url
url = config_parser.get("news_config","url")
driver.find_elements_by_tag_name("input")[1].send_keys("https://www.huanqiu.com/")

# 过滤信息
filter = config_parser.get("news_config","filter")
driver.find_elements_by_tag_name("input")[2].send_keys(filter)

#筛选新闻类型 
new_type = config_parser.get("news_config","news_type")
driver.find_elements_by_tag_name("input")[3].send_keys(new_type)

#相对路径
pre_url = config_parser.get("news_config","pre_url")
driver.find_elements_by_tag_name("input")[4].send_keys(pre_url)

#文章样式
article_class = config_parser.get("news_config","article")
driver.find_elements_by_tag_name("input")[5].send_keys(article_class)

#标题样式
title_class = config_parser.get("news_config","title")
driver.find_elements_by_tag_name("input")[6].send_keys(title_class)

time.sleep(3)

driver.find_elements_by_tag_name("uni-button")[1].click()

time.sleep(60)

#保存至数据库 
driver.find_elements_by_tag_name("uni-button")[2].click()
time.sleep(3)

driver.find_elements_by_class_name("uni-modal__btn_primary")[0].click()
time.sleep(3)

driver.quit()

